import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from "path"

// 自定义 rpx 转 px 插件
function rpxToPxPlugin() {
  return {
    name: "vite-plugin-rpx-to-px",
    enforce: "pre",
    transform(code, id) {
      // console.log('code',code);
      if (id.endsWith(".css") || id.endsWith(".scss") || id.endsWith(".less") || id.endsWith(".jsx")) {
        // 将 rpx 替换为 px，假设 1rpx = 1px（可根据需要调整比例）
        return code.replace(/\b(\d+(\.\d+)?)rpx\b/g, (match, value) => {
          return `${value}rem`;
        });
      }
      return code;
    },
  };
}

// https://vite.dev/config/
export default defineConfig({
  base:"./",
  plugins: [
    rpxToPxPlugin(),
    react(),
  ],
  resolve:{
    alias:{
      "@/assets":path.resolve(__dirname,"src/assets")
    }
  }
})
